$(function() {
	
	var name = $("#resume_names");
	$("#resumeSearch").on('click', function() {
		$("#recepResume").datagrid("load", {
			resumeName : "%" + name.val() + "%"
		});
	});

	var form = $("#greatsearchForm");
	$("#resumeReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		$("#recepResume").datagrid("load", {});
	});

	$("#skills").combobox({
		url : '/resumes/language',
		valueField : 'languageName',
		textField : 'languageName',
		panelHeight : 'auto',
		multiple : true,
		multiline : true,
		editable : false,
		onClick : function() {
			var roadIdName = [];
			var length = $("input[name='skills']").length;
			$("input[name='skills']").each(function() {
				roadIdName.push($(this).val());
			});
			var c = $("input[name='skills']:eq(" + (length - 1) + ")").val();// 获取最后点击的元素
			if (length > 4) {
				roadIdName.splice($.inArray(c, roadIdName), 1);// 移除数组中的指定元素
				$("#skills").combobox('setValues', roadIdName);
			}
		},
	});
});
function deleteResume(id) {
	$.messager.confirm("提示", "您确定要删除此简历吗？", function(r) {
		if (r) {
			$.get("../../resumes/deleteresume/" + id, function(rs) {
				if (rs.success) {
					window.location.reload();
				} else {
					var obj = JSON.parse(rs);
					$.messager.alert("提示", obj.message);
				}
			});
		}
	});
}

function deleteWork(id) {
	$.messager.confirm("提示", "您确定要删除此记录吗？", function(r) {
		if (r) {
			$.get("../../resumes/deletework/" + id, function(rs) {
				if (rs.success) {
					window.location.reload();
				} else {
					var obj = JSON.parse(rs);
					$.messager.alert("提示", obj.message);
				}
			});
		}
	});
}
function deleteProject(id) {
	$.messager.confirm("提示", "您确定要删除此记录吗？", function(r) {
		if (r) {
			$.get("../../resumes/deleteproject/" + id, function(rs) {
				if (rs.success) {
					window.location.reload();
				} else {
					var obj = JSON.parse(rs);
					$.messager.alert("提示", obj.message);
				}
			});
		}
	});
}

function myresume(url, title) {
	var tab = $('#resumetabs').tabs('getSelected');
	if (url != null) {
		$("#resumetabs").tabs('update', {
			tab : tab,
			options : {
				title : title,
				showHeader : 'false',
				closable : true,
				border : 'false',
				href : url
			}
		});
		tab.panel('refresh', url);
	}
}
function addresume(userid, id) {
	// 创建窗口
	var dialog = $("<div/>").dialog(
			{
				href : '../../resumes/editresume/' + userid + '/'
						+ (id ? ("/" + id) : ""),
				title : id ? "编辑简历" : "创建简历",
				height : 360,
				width : 695,
				modal : true,
				onClose : function() {
					// 窗口关闭的同时销毁此窗口
					$(this).dialog("destroy");
				},
				buttons : [ {
					iconCls : 'icon-save',
					text : '确定',
					handler : function() {
						$("#editresumeForm").form(
								'submit',
								{
									url : '../../resumes/addmineresume',
									data : $("#editresumeForm").serialize(),
									success : function(success) {
										var rs = JSON.parse(success);
										if (rs.success) {
											window.location.reload();
											dialog.dialog("close");
										} else {
											$.messager.alert("提示", rs.message);
										}
									}
								});
					}
				} ]
			});
}

function addwork(resumeid, id) {
	// 创建窗口
	var dialog = $("<div/>").dialog(
			{
				href : '../../resumes/getworks/' + resumeid + '/'
						+ (id ? ("/" + id) : ""),
				title : id ? "编辑工作经验" : "创建工作经验",
				height : 330,
				width : 370,
				modal : true,
				onClose : function() {
					// 窗口关闭的同时销毁此窗口
					$(this).dialog("destroy");
				},
				buttons : [ {
					iconCls : 'icon-save',
					text : '确定',
					handler : function() {
						$("#takeWorksForm").form(
								'submit',
								{
									url : '../../resumes/addworks',
									data : $("#takeWorksForm").serialize(),
									success : function(success) {
										var rs = JSON.parse(success);
										if (rs.success) {
											window.location.reload();
											dialog.dialog("close");
										} else {
											$.messager.alert("提示", rs.message);
										}
									}
								});
					}
				} ]
			});
}

function addproject(resumeid, id) {
	// 创建窗口
	var dialog = $("<div/>").dialog({
				href : '../../resumes/getprojects/' + resumeid + '/' + (id ? ("/" + id) : ""),
				title : id ? "编辑项目经验" : "创建项目经验",
				height : 330,
				width : 370,
				modal : true,
				onClose : function() {
					// 窗口关闭的同时销毁此窗口
					$(this).dialog("destroy");
				},
				buttons : [ {
					iconCls : 'icon-save',
					text : '确定',
					handler : function() {
					$("#takeProjectsForm").form('submit',{
							url : '../../resumes/addprojects',
							data : $("#takeProjectsForm").serialize(),
							success : function(success) {
							var rs = JSON.parse(success);
						if (rs.success) {
							window.location.reload();
							dialog.dialog("close");
						} else {
							$.messager.alert("提示", rs.message);
						}
					}
				});
			}
		} ]
	});
}


$(function() {
	var recepResume = $("#recepResume");
	recepResume.datagrid({
		rownumbers:true,
		pagination:true,
		singleSelect:true,
		border:false,
		url:'resumes/recommend',
		columns: [
			[{
				field:'resume_name',
				width:200,
				align:'center',
				title: '姓名'
			}, {
				field:'title',
				width:400,
				title: '简历标题',
			}, {
				field: 'option',
				title: '&nbsp;',
				width: 200,
				align: 'center',
				formatter: function(value, row, index) {
					var bts = [];
					bts.push("<a target='_blank' href='resumes/preview/" + row.tb_resume_id
							+ "' data-id='" + row.tb_resume_id + "' data-index='" + index
							+ "' class='ctr'>详情</a>");
					return bts.join("");
				}
			}]
		]
	});
});
